package com.kingbook.api.rest;

import com.google.common.collect.Lists;
import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONResult;
import com.kingbook.services.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.kingbook.core.auth.LoginManager.AUTH;

/**
 * @Author Bucky
 * @Date 2020-01-08 21:13:15
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("images")
public class ImagesRest {

    @Autowired
    ImagesService imagesService;

    @Autowired
    WorkorderService workorderService;

    @Autowired
    MasterService masterService;

    @Autowired
    CustomerService customerService;

    @Autowired
    PartnerService partnerService;

    @Autowired
    VehicleService vehicleService;

    @Autowired
    WorkspaceService workspaceService;

    @Autowired
    DriverService driverService;

    @GetMapping("order/{id}")
    public JSONResult getOrderImages(@PathVariable int id) {
        return imagesService.getOrderImages(id);
    }

    @GetMapping("vehicle/{id}")
    public JSONResult getVehicleImages(@PathVariable int id) {
        return imagesService.getVehicleImages(id);
    }

    @GetMapping("workspace/{id}")
    public JSONResult getWorkspaceImages(@PathVariable int id) {
        return imagesService.getWorkspaceImages(id);
    }

    @PutMapping("order")
    public JSONResult saveOrderImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        param.put("mid", LoginManager.getMid(auth));
        int oid = MapUtils.getIntValue(param, "oid");
        if (workorderService.getRows(oid, mid) == 1) {
            List<Map<String, Object>> images = (List<Map<String, Object>>) MapUtils.getObject(param, "images", Lists.newArrayList());
            List<Map<String, ?>> list = new ArrayList<>();
            for (Map<String, Object> image : images) {
                image.put("mid", mid);
                image.put("oid", oid);
                list.add(image);
            }
            return imagesService.saveOrderImages(param, list);
        }
        return JSONResult.failed("用户订单不存在");
    }

    @PutMapping("vehicle")
    public JSONResult saveVehicleImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        param.put("mid", LoginManager.getMid(auth));
        int vid = MapUtils.getIntValue(param, "vid");
        if (vehicleService.getRows(vid, mid) == 1) {
            List<Map<String, Object>> images = (List<Map<String, Object>>) MapUtils.getObject(param, "images", Lists.newArrayList());
            List<Map<String, ?>> list = new ArrayList<>();
            for (Map<String, Object> image : images) {
                image.put("mid", mid);
                image.put("vid", vid);
                list.add(image);
            }
            return imagesService.saveVehicleImages(param, list);
        }
        return JSONResult.failed("车辆不存在");
    }

    @PutMapping("workspace")
    public JSONResult saveWorkspaceImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        param.put("mid", LoginManager.getMid(auth));
        int wid = MapUtils.getIntValue(param, "wid");
        if (workspaceService.getRows(wid, mid) == 1) {
            List<Map<String, Object>> images = (List<Map<String, Object>>) MapUtils.getObject(param, "images", Lists.newArrayList());
            List<Map<String, ?>> list = new ArrayList<>();
            for (Map<String, Object> image : images) {
                image.put("mid", mid);
                image.put("wid", wid);
                list.add(image);
            }
            return imagesService.saveWorkspaceImages(param, list);
        }
        return JSONResult.failed("工地不存在");
    }

    @PutMapping("avatar")
    @ContainProperties({"id", "image"})
    public JSONResult saveAvatarImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("id", LoginManager.getMid(auth));
        param.put("avatarUrl", MapUtils.getString(param, "image"));
        return masterService.modify(param);
    }

    @PutMapping("customer")
    @ContainProperties({"id", "image"})
    public JSONResult saveCustomerImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        param.put("id", MapUtils.getIntValue(param, "id"));
        param.put("avatarUrl", MapUtils.getString(param, "image"));
        return customerService.modify(param);
    }

    @PutMapping("partner")
    @ContainProperties({"id", "image"})
    public JSONResult savePartnerImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        param.put("id", MapUtils.getIntValue(param, "id"));
        param.put("avatarUrl", MapUtils.getString(param, "image"));
        return partnerService.modify(param);
    }

    @PutMapping("driver")
    @ContainProperties({"id", "image"})
    public JSONResult saveDriverImages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        param.put("id", MapUtils.getIntValue(param, "id"));
        param.put("avatarUrl", MapUtils.getString(param, "image"));
        return driverService.modify(param);
    }
}
